package com.ulife.permission.manager.impl;

import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;

import com.ulife.common.Result;
import com.ulife.common.exception.ManagerException;
import com.ulife.permission.dao.TargetDao;
import com.ulife.permission.dao.dataobject.TargetDO;
import com.ulife.permission.manager.TargetManager;

public class TargetManagerImpl implements TargetManager {
	
	@Autowired
	private TargetDao targetDao;

	public Result<TargetDO> getTargetByTargetId(String targetId) throws ManagerException {
		Result<TargetDO> result = new Result<TargetDO>();
		try {
			TargetDO target = targetDao.getTargetByTargetId(targetId);
			if (target != null) {
				result.setSuccess(true);
				result.setModule(target);
			}
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

	public Result<Integer> updateTarget(TargetDO target) throws ManagerException {
		Result<Integer> result = new Result<Integer>();
		try {
			int i = targetDao.updateTarget(target);
			if (i > 0) {
				result.setSuccess(true);
			}
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

	public Result<Integer> addTarget(TargetDO targetDO)throws ManagerException {
		Result<Integer> result = new Result<Integer>();
		try {
			int id = targetDao.addTarget(targetDO);
			if (id > 0) {
				result.setSuccess(true);
			}
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

}
